import Vue from "vue"
import VueRouter from "vue-router"

import Home from "../containers/Home"
import Enjoy from "../containers/Enjoy"
import National from "../containers/National"
import Login from "../containers/Login"

// import Shanxi from "../containers/subpage/shanxi"
// import Neimeng from "../containers/subpage/neimeng"

const Shanxi = () => import("../containers/subpage/shanxi");
const Neimeng = () => import("../containers/subpage/neimeng");

Vue.use(VueRouter)

export default new VueRouter({
    linkActiveClass: "active",
    linkExactActiveClass: "exactActive",
    mode:"history",
    scrollBehavior(to, from, savedPosition) {
        // if (savedPosition) {
        //     return savedPosition
        // } else {
        //     return { x: 0, y: 0 }
        // }
        // if(to.meta.scrollTop){
        //     return {
        //         x:0,
        //         y:to.meta.scrollTop
        //     }
        // }else{
        //     return { x: 0, y: 0 }
        // }
        return new Promise((resolve, reject) => {
            if (to.meta.scrollTop) {
                setTimeout(() => {
                    resolve({ x: 0, y: to.meta.scrollTop })
                }, 300)
            } else {
                return { x: 0, y: 0 }
            }
        })
    },
    routes: [
        {
            path: "/",
            component: Home,
            meta: { requiresAuth: false, hello: "hello", scrollTop: 100 }
        },
        {
            path: "/login",
            component: Login,
            meta: { requiresAuth: false }
        },
        {
            path: "/enjoy/hotnews/:content",
            name: "Enjoy",
            component: Enjoy,
            // 路由独享守卫
            // beforeEnter:(to, from, next) => {
            //     // console.log(from);
            //     // console.log(to);
            //     next();
            // }
            // requiresAuth(自定义字段):true，此页面需要登陆才能跳转打开
            meta: { requiresAuth: true }
        },
        {
            path: "/national",
            component: National,
            meta: { requiresAuth: true },
            // 重定向
            redirect: "/national/shanxi",
            children: [
                {
                    // 子路由路径不需要添加/
                    path: "shanxi",
                    component: Shanxi,
                    meta: { requiresAuth: false }
                },
                {
                    path: "neimeng",
                    component: Neimeng,
                    meta: { requiresAuth: true }
                }
            ]
        }
    ]
})